﻿using System;
using System.Collections;//Please add references
using System.Data;
using System.Data.SQLite;
using System.Text;
using EIM_Accounting.Common.DBUtility;

namespace Accounting.DAL
{
    /// <summary>
    /// 数据访问类:WagesProvide
    /// </summary>
    public partial class WagesDetail
    {
        public WagesDetail()
        { }

        #region Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from WagesDetail");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Accounting.Model.WagesDetail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into WagesDetail(");
            strSql.Append("StaffID,AttendanceMonth,LegalDays,FactDays,BaseMoney,AddWork,AddWorkBaseMoney,Award,Punishment,Remark)");
            strSql.Append(" values (");
            strSql.Append("@StaffID,@AttendanceMonth,@LegalDays,@FactDays,@BaseMoney,@AddWork,@AddWorkBaseMoney,@Award,@Punishment,@Remark)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,8),
                    new SQLiteParameter("@AttendanceMonth", DbType.String),
                    new SQLiteParameter("@LegalDays", DbType.Decimal,4),
                    new SQLiteParameter("@FactDays", DbType.Decimal,4),
                    new SQLiteParameter("@BaseMoney", DbType.Decimal,4),
                    new SQLiteParameter("@AddWork", DbType.Decimal,4),
                    new SQLiteParameter("@AddWorkBaseMoney", DbType.Decimal,4),
                    new SQLiteParameter("@Award", DbType.Decimal,4),
                    new SQLiteParameter("@Punishment", DbType.Decimal,4),
                    new SQLiteParameter("@Remark", DbType.String)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.AttendanceMonth;
            parameters[2].Value = model.LegalDays;
            parameters[3].Value = model.FactDays;
            parameters[4].Value = model.BaseMoney;
            parameters[5].Value = model.AddWork;
            parameters[6].Value = model.AddWorkBaseMoney;
            parameters[7].Value = model.Award;
            parameters[8].Value = model.Punishment;
            parameters[9].Value = model.Remark;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Accounting.Model.WagesDetail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update WagesDetail set ");
            strSql.Append("StaffID=@StaffID,");
            strSql.Append("AttendanceMonth=@AttendanceMonth,");
            strSql.Append("LegalDays=@LegalDays,");
            strSql.Append("FactDays=@FactDays,");
            strSql.Append("BaseMoney=@BaseMoney,");
            strSql.Append("AddWork=@AddWork,");
            strSql.Append("AddWorkBaseMoney=@AddWorkBaseMoney,");
            strSql.Append("Award=@Award,");
            strSql.Append("Punishment=@Punishment,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,8),
                    new SQLiteParameter("@AttendanceMonth", DbType.String),
                    new SQLiteParameter("@LegalDays", DbType.Decimal,4),
                    new SQLiteParameter("@FactDays", DbType.Decimal,4),
                    new SQLiteParameter("@BaseMoney", DbType.Decimal,4),
                    new SQLiteParameter("@AddWork", DbType.Decimal,4),
                    new SQLiteParameter("@AddWorkBaseMoney", DbType.Decimal,4),
                    new SQLiteParameter("@Award", DbType.Decimal,4),
                    new SQLiteParameter("@Punishment", DbType.Decimal,4),
                    new SQLiteParameter("@Remark", DbType.String),
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.AttendanceMonth;
            parameters[2].Value = model.LegalDays;
            parameters[3].Value = model.FactDays;
            parameters[4].Value = model.BaseMoney;
            parameters[5].Value = model.AddWork;
            parameters[6].Value = model.AddWorkBaseMoney;
            parameters[7].Value = model.Award;
            parameters[8].Value = model.Punishment;
            parameters[9].Value = model.Remark;
            parameters[10].Value = model.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WagesDetail ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WagesDetail ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Accounting.Model.WagesDetail GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,AttendanceMonth,LegalDays,FactDays,BaseMoney,AddWork,AddWorkBaseMoney,Award,Punishment,Remark from WagesDetail ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            Accounting.Model.WagesDetail model = new Accounting.Model.WagesDetail();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StaffID"] != null && ds.Tables[0].Rows[0]["StaffID"].ToString() != "")
                {
                    model.StaffID = int.Parse(ds.Tables[0].Rows[0]["StaffID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AttendanceMonth"] != null && ds.Tables[0].Rows[0]["AttendanceMonth"].ToString() != "")
                {
                    model.AttendanceMonth = ds.Tables[0].Rows[0]["AttendanceMonth"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LegalDays"] != null && ds.Tables[0].Rows[0]["LegalDays"].ToString() != "")
                {
                    model.LegalDays = decimal.Parse(ds.Tables[0].Rows[0]["LegalDays"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FactDays"] != null && ds.Tables[0].Rows[0]["FactDays"].ToString() != "")
                {
                    model.FactDays = decimal.Parse(ds.Tables[0].Rows[0]["FactDays"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BaseMoney"] != null && ds.Tables[0].Rows[0]["BaseMoney"].ToString() != "")
                {
                    model.BaseMoney = decimal.Parse(ds.Tables[0].Rows[0]["BaseMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddWork"] != null && ds.Tables[0].Rows[0]["AddWork"].ToString() != "")
                {
                    model.AddWork = decimal.Parse(ds.Tables[0].Rows[0]["AddWork"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddWorkBaseMoney"] != null && ds.Tables[0].Rows[0]["AddWorkBaseMoney"].ToString() != "")
                {
                    model.AddWorkBaseMoney = decimal.Parse(ds.Tables[0].Rows[0]["AddWorkBaseMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Award"] != null && ds.Tables[0].Rows[0]["Award"].ToString() != "")
                {
                    model.Award = decimal.Parse(ds.Tables[0].Rows[0]["Award"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Punishment"] != null && ds.Tables[0].Rows[0]["Punishment"].ToString() != "")
                {
                    model.Punishment = decimal.Parse(ds.Tables[0].Rows[0]["Punishment"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,AttendanceMonth,LegalDays,FactDays,BaseMoney,AddWork,AddWorkBaseMoney,Award,Punishment,Remark ");
            strSql.Append(" FROM WagesDetail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "WagesDetail";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method

        internal Model.WagesDetail GetModel(int staffID, string calcMonth)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,AttendanceMonth,LegalDays,FactDays,BaseMoney,AddWork,AddWorkBaseMoney,Award,Punishment,Remark from WagesDetail ");
            strSql.Append(" where StaffID=@StaffID");
            strSql.Append(" and AttendanceMonth=@AttendanceMonth");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,4),
                    new SQLiteParameter("@AttendanceMonth", DbType.String)
};
            parameters[0].Value = staffID;
            parameters[1].Value = calcMonth;

            Accounting.Model.WagesDetail model = new Accounting.Model.WagesDetail();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StaffID"] != null && ds.Tables[0].Rows[0]["StaffID"].ToString() != "")
                {
                    model.StaffID = int.Parse(ds.Tables[0].Rows[0]["StaffID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AttendanceMonth"] != null && ds.Tables[0].Rows[0]["AttendanceMonth"].ToString() != "")
                {
                    model.AttendanceMonth = ds.Tables[0].Rows[0]["AttendanceMonth"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LegalDays"] != null && ds.Tables[0].Rows[0]["LegalDays"].ToString() != "")
                {
                    model.LegalDays = decimal.Parse(ds.Tables[0].Rows[0]["LegalDays"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FactDays"] != null && ds.Tables[0].Rows[0]["FactDays"].ToString() != "")
                {
                    model.FactDays = decimal.Parse(ds.Tables[0].Rows[0]["FactDays"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BaseMoney"] != null && ds.Tables[0].Rows[0]["BaseMoney"].ToString() != "")
                {
                    model.BaseMoney = decimal.Parse(ds.Tables[0].Rows[0]["BaseMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddWork"] != null && ds.Tables[0].Rows[0]["AddWork"].ToString() != "")
                {
                    model.AddWork = decimal.Parse(ds.Tables[0].Rows[0]["AddWork"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddWorkBaseMoney"] != null && ds.Tables[0].Rows[0]["AddWorkBaseMoney"].ToString() != "")
                {
                    model.AddWorkBaseMoney = decimal.Parse(ds.Tables[0].Rows[0]["AddWorkBaseMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Award"] != null && ds.Tables[0].Rows[0]["Award"].ToString() != "")
                {
                    model.Award = decimal.Parse(ds.Tables[0].Rows[0]["Award"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Punishment"] != null && ds.Tables[0].Rows[0]["Punishment"].ToString() != "")
                {
                    model.Punishment = decimal.Parse(ds.Tables[0].Rows[0]["Punishment"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        internal void AddBatch(Model.WagesDetail model, Model.WagesProvide modelProvide)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into WagesDetail(");
            strSql.Append("StaffID,AttendanceMonth,LegalDays,FactDays,BaseMoney,AddWork,AddWorkBaseMoney,Award,Punishment,Remark)");
            strSql.Append(" values (");
            strSql.Append(model.StaffID).Append(",'");
            strSql.Append(model.AttendanceMonth).Append("',");
            strSql.Append(model.LegalDays).Append(",");
            strSql.Append(model.FactDays).Append(",");
            strSql.Append(model.BaseMoney).Append(",");
            strSql.Append(model.AddWork).Append(",");
            strSql.Append(model.AddWorkBaseMoney).Append(",");
            strSql.Append(model.Award).Append(",");
            strSql.Append(model.Punishment).Append(",'");
            strSql.Append(model.Remark).Append("')");
            strSql.Append(";select LAST_INSERT_ROWID()");

            ArrayList listSql = new ArrayList();
            listSql.Add(strSql.ToString());

            strSql = new StringBuilder();
            strSql.Append("insert into WagesProvide(");
            strSql.Append("StaffID,ProvideMonth,BaseWage,Award,Punishment,AddWork,Remark,ProvideFee)");
            strSql.Append(" values (");
            strSql.Append(modelProvide.StaffID).Append(",'");
            strSql.Append(modelProvide.ProvideMonth).Append("',");
            strSql.Append(modelProvide.BaseWage).Append(",");
            strSql.Append(modelProvide.Award).Append(",");
            strSql.Append(modelProvide.Punishment).Append(",");
            strSql.Append(modelProvide.AddWork).Append(",'");
            strSql.Append(modelProvide.Remark).Append("',");
            strSql.Append(modelProvide.ProvideFee).Append(")");
            strSql.Append(";select LAST_INSERT_ROWID()");

            listSql.Add(strSql.ToString());
            DbHelperSQLite.ExecuteSqlTran(listSql);
        }

        internal void UpdateBatch(Model.WagesDetail model, Model.WagesProvide modelProvide)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update WagesDetail set ");
            strSql.Append("StaffID=" + model.StaffID + ",");
            strSql.Append("AttendanceMonth='" + model.AttendanceMonth + "',");
            strSql.Append("LegalDays=" + model.LegalDays + ",");
            strSql.Append("FactDays=" + model.FactDays + ",");
            strSql.Append("BaseMoney=" + model.BaseMoney + "");
            if (model.AddWork != null)
                strSql.Append(",AddWork=" + model.AddWork + "");
            if (model.AddWorkBaseMoney != null)
                strSql.Append(",AddWorkBaseMoney=" + model.AddWorkBaseMoney);
            if (model.Award != null)
                strSql.Append(",Award=" + model.Award);
            if (model.Punishment != null)
                strSql.Append(",Punishment=" + model.Punishment);
            if (model.Remark != null)
                strSql.Append(",Remark='" + model.Remark);
            strSql.Append(" where ID=" + model.ID);

            ArrayList listSql = new ArrayList();
            listSql.Add(strSql.ToString());

            strSql = new StringBuilder();
            strSql.Append("update WagesProvide set ");
            strSql.Append("StaffID=" + modelProvide.StaffID + ",");
            strSql.Append("ProvideMonth=" + modelProvide.ProvideMonth + ",");
            strSql.Append("BaseWage=" + modelProvide.BaseWage + ",");
            if (modelProvide.Award != null)
                strSql.Append("Award=" + modelProvide.Award + ",");
            if (modelProvide.Punishment != null)
                strSql.Append("Punishment=" + modelProvide.Punishment + ",");
            if (modelProvide.AddWork != null)
                strSql.Append("AddWork=" + modelProvide.AddWork + ",");
            if (modelProvide.Remark != null)
                strSql.Append("Remark=" + modelProvide.Remark + ",");
            strSql.Append("ProvideFee=" + modelProvide.ProvideFee + ",");
            strSql.Append(" where ID=" + modelProvide.ID);

            listSql.Add(strSql.ToString());
            DbHelperSQLite.ExecuteSqlTran(listSql);
        }
    }
}